package com.sm.controller;

import com.sm.model.MyVip;
import com.sm.model.User;
import com.sm.service.MyVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sm.service.CollectService;
import com.sm.service.CouponService;
import com.sm.service.PersonalHomeService;
import com.sm.util.Msg;

import java.util.List;

/**
 *	个人中心的展示页的controller
 * @author 60915
 *
 */
@Controller
@RequestMapping("/persona")
public class PersonalHomeController {

	@Autowired
	private PersonalHomeService personalHomeService;
	@Autowired
	private CouponService couponService;
	@Autowired
	private CollectService collectService;
	@Autowired
	private MyVipService myVipService;

	/**
	 * 个人中心的展示页
	 * @param userId	用户id
	 * @return
	 */
	@RequestMapping("/personalCenter")
	@ResponseBody
	public Msg PersonalCenter(Integer userId) {
		//通过用户id查询用户的信息。
		User user = personalHomeService.selectByPrimaryKey(userId);
		User parentUser = personalHomeService.selectByPrimaryKey(user.getRefereesUserId());
		user.setParentCode(parentUser==null?null:parentUser.getRecommendCode());
		//设置最高VIP等级
		user.setBestVip(myVipService.getMyTopType(userId));
		//查询用户的所有的收藏的数量。
		int collectNumber = collectService.selectCollectNumber(userId);
		//通过用户id查询用户的优惠券张数
		int number = couponService.couponNumber(userId);
		//会员特权的数量
		List<MyVip> myVip = myVipService.getMyVip(userId);
		return Msg.success().add("user", user).add("collectNumber", collectNumber).add("number", number).add("vipNumber", myVip == null ? 0 : myVip.size());
	}


}
